<template>
  <div class="csutom-color-picker">
  <a-popover placement="bottom" trigger="click">
    <template slot="content">
      <component
        :is="colorComponent"
        v-model="themeColor"
        @input="handleColorChange"
        ></component>
    </template>
    <div class="color-picker-trigger">
      <div class="color-show" :style="{ background: themeColor }">
        <a-icon type="down" />
      </div>
    </div>
  </a-popover>
  </div>
</template>

<script>
export default {
  name: 'ColorPicker',
  props: {
    color: {
      type: String,
      default: '#000'
    }
  },
  model: {
    event: 'input',
    prop: 'color'
  },

  data () {
    return {
      colorComponent: null,
      themeColor: '#000'
    }
  },

  mounted () {
    const { Sketch } = require('vue-color')
    this.colorComponent = Sketch
  },

  methods: {
    handleColorChange (e) {
      this.$emit('input', e.hex8)
    }
  },

  watch: {
    color: {
      handler (newVal) {
        this.themeColor = newVal
      },
      immediate: true
    }
  }
}
</script>
<style lang="less" scoped>
.csutom-color-picker {
  .color-picker-trigger {
    width: 30px;
    height: 30px;
    cursor: pointer;
    padding: 4px;
    border: 2px;
    border: 1px solid #f1f1f1;
    .color-show {
      width: 100%;
      height: 100%;
      background-color: #000;
      border-radius: 2px;
      position: relative;
      line-height: 22px;
      text-align: center;
      color: #fff;
      font-size: 10px;
    }
  }
}
</style>
